package twitter.client.listener;

import twitter.client.bean.TimelineStatus;
import twitter.client.task.GetTimelineTask;
import twitter.client.util.Global;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

public class TweetListOnScrollListener implements OnScrollListener {

	public TweetListOnScrollListener() {
		// this.task = new QuerySearchTask(view);
	}

	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		Log.d("onScroll", firstVisibleItem + " : " + visibleItemCount + " : "
				+ totalItemCount);

		GetTimelineTask task = Global.timelineTask;
		if (task == null) {
			task = new GetTimelineTask(view);
			Global.timelineTask = task;
		}

		if (totalItemCount != 0
				&& firstVisibleItem + visibleItemCount == totalItemCount) {
			TimelineStatus tlStat = Global.timelineStatus;
			if (tlStat.isLast()) {
				return;
			}
			AsyncTask.Status status = task.getStatus();
			Log.d("onScroll", "task status : " + status);
			if (AsyncTask.Status.RUNNING.equals(status)) {
				return;
			}
			if (AsyncTask.Status.FINISHED.equals(status)) {
				task = new GetTimelineTask(view);
				Global.timelineTask = task;
			}

			((ListView) view).addFooterView(Global.loadingFooterView);

			task.execute(Global.currentTwitter);
		}
	}

	public void onScrollStateChanged(AbsListView view, int scrollState) {
		Log.d("onScrollStateChanged", scrollState + "");
	}
}
